﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;


namespace mojoPortal.Business
{
    public class SiteUser : System.Web.UI.UserControl
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string LoginName { get; set; }
        public string Password { get; set; }
        public string MobilePin { get; set; }
        public string SSOToken { get; set; }

        public SiteUser()
        {
            string username = "appollohy";
            string password = "password123";
            string pin = "123456789b";
            
            //string username = "anshuman";
            //string password = "password123";
            //string pin = "123456789f";

            SetSiteUserCredential(username, password, pin);
        }
        public string GetCustomPropertyAsString(string propertyname)
        {
            if (propertyname.ToLower() == "registrationpin")
                return MobilePin;
            if (propertyname.ToLower() == "employeeid")
                return UserId.ToString();
            return "";
        }
        private void SetSiteUserCredential(string username, string password, string pin)
        {
            if (username.Length > 0 && password.Length > 0 && pin.Length > 0)
            {
                String strSQL = "SELECT e.EmployeeId,e.FirstName,e.MiddleName,e.LastName,e.Email,e.State,e.UserName,e.Token,e.Password,e.PIN,e.IsActive,e.CompanyId,c.CompanyName FROM rs_tblEmployee e inner join rs_tblCompany c on e.companyid=c.companyid where e.username=@username and e.password=@password and e.pin=@pin and e.isactive=1 and e.enablelogin=1";
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
                myCommand = new SqlCommand(strSQL, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@username";
                param.Value = username;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@password";
                param.Value = password;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.NVarChar;
                param.ParameterName = "@pin";
                param.Value = pin;
                myCommand.Parameters.Add(param);

                LoginName = username;
                Password = password;
                MobilePin = pin;

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    Email = myDataReader["Email"].ToString();
                    FirstName = myDataReader["FirstName"].ToString();
                    LastName = myDataReader["LastName"].ToString();
                    UserId = Convert.ToInt32(myDataReader["EmployeeId"].ToString());
                    //LoginName = myDataReader["UserName"].ToString();
                    //Password = myDataReader["Password"].ToString();
                    //MobilePin = myDataReader["PIN"].ToString();
                    SSOToken = myDataReader["Token"].ToString();
                }
                myConnection.Close();
            }
        }


        public string GetRSCompanyConnectionString(string PIN)
        {
            string rsCompanyConnectionString = "";

            String strSQL = "select e.companyid, c.connectionstring from rs_tblEmployee e inner join rs_tblcompany c on e.companyid=c.companyid where e.PIN=@pin and e.EnableLogin=1 and e.IsActive=1";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.NVarChar;
            param.ParameterName = "@pin";
            param.Value = PIN;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            if (myDataReader.HasRows)
            {
                myDataReader.Read();
                rsCompanyConnectionString = myDataReader["ConnectionString"].ToString();
            }
            myConnection.Close();

            return rsCompanyConnectionString;
        }

    }
}